<!DOCTYPE html>
<html>

<head>
    <title>Preview Display</title>
    <meta charset="UTF-8">
    <link rel="shortcut icon" href="custom/images/favicon.ico?v=2" />
    <style>
        html,
        body {
            padding: 0px;
            margin: 0px;
        }
    </style>
    <script src='../libs/jquery-3.4.1.min.js'></script>
    <script src='../configs/htconfig.js'></script>
    <script src='../libs/echarts.js'></script>
    <script src='../libs/core/IZrowIfUVaXF.js'></script>
    <script src='../libs/core/VT3MIEkV8AKv.js'></script>
    <script src='../libs/core/ht.js'></script>
    <script src='../libs/core/buckle.js'></script>
    <script src='../libs/plugin/ht-edgetype.js'></script>
    <script src='../libs/plugin/ht-obj.js'></script>
    <script src='../libs/core/ht-ui.js'></script>
    <script src='../libs/plugin/ht-form.js'></script>
    <script src='../libs/plugin/ht-vector.js'></script>

    <script src='ajax/ajax.js'></script>
    <script src='ajax/jquery.table2excel.js'></script>
    <script>
        var dataModel = new ht.DataModel();
        graphView = new ht.graph.GraphView(dataModel);


        function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) { return pair[1]; }
            }
            return (undefined);
        }


        function init() {
            graphView.addToDOM();
            ht.Default.xhrLoad("displays/jibei/功率特性曲线机组.json", function (text) {
                var json = ht.Default.parse(text);
                if (json.title) document.title = json.title;

                dataModel.deserialize(json);
                getData(dataModel);//页面取值赋值
                graphView.fitContent(true)
            });
        }

        function getData(dataModel) {
            var area = dataModel.getDataByTag("area");//区域
            var station = dataModel.getDataByTag("station");//场站
            var fan = dataModel.getDataByTag("fan");//机组
            var startTime = dataModel.getDataByTag("startTime");//开始时间
            var endTime = dataModel.getDataByTag("endTime");//结束时间

            var areaData = getArea();//获取区域名称以及id

            if (areaData != "") {
                area.a("ht.dataSource", areaData)
            }

            var jizu = getQueryVariable("unitId") == undefined ? 1162 : getQueryVariable("unitId");//从地址栏获取机组信息
            var areaValue = getQueryVariable("areaId") == undefined ? "" : getQueryVariable("areaId");
            var stationValue = getQueryVariable("stationId") == undefined ? 610 : getQueryVariable("stationId");
            var start = getQueryVariable("start");
            var end = getQueryVariable("end");

            var stationArr = getStation(areaValue);
            station.a("ht.dataSource", stationArr)
            var fanArr = getFan(stationValue);
            fan.a("ht.dataSource", fanArr)

            // 设置下拉框默认值
            if (areaValue != "") {
                area.a("ht.value", parseInt(areaValue));
            } else {
                area.a("ht.value", "");
            }

            station.a("ht.value", parseInt(stationValue));
            fan.a("ht.value", parseInt(jizu));//地址栏传参附默认值
            startTime.a("ht.value", parseTime(start));
            endTime.a("ht.value", parseTime(end));

            unitPowerInfo(jizu, start, end)//机组风速功率信息 表格数据
            curveEvaluateInfo(jizu, start, end)//机组功率曲线评价信息
            unitPowerCurveInfo(jizu, start, end)//功率曲线

            // }


        }
        function parseTime(time) {
            var mydate = new Date(parseInt(time));
            var year = mydate.getFullYear();
            var month = mydate.getMonth() + 1;
            var day = mydate.getDate();

            return year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);
        }




        //上传文件
        function getFileName(path) {//获取文件名
            var pos1 = path.lastIndexOf('/');
            var pos2 = path.lastIndexOf('\\');
            var pos = Math.max(pos1, pos2)
            if (pos < 0)
                return path;
            else
                return path.substring(pos + 1);
        }
        //上传文件onchange触发的事件
        function showsize(files) {

            var filename = getFileName(files.value);
            var ext = filename.substr(filename.lastIndexOf(".")).toLowerCase()//获取文件名后缀
            if (ext != ".xls" && ext != ".xlsx") {
                alert("文件格式错误，要求是excel文件");
                return false;
            }
            checkFileSize(files);//校验文件大小
        }
        //校验文件大小
        function checkFileSize(files) {
            var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
            var fileSize = 0;
            if (isIE && !files) {
                var filePath = files.value;
                var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
                var file = fileSystem.GetFile(filePath);
                fileSize = files.size;
            } else {
                fileSize = files.size;
            }
            var fsize = fileSize / (1024 * 1024);
            if (fsize < 60) {//上传的文件
                upload();//调用ajax的上传事件
            } else {
                alert("文件太大，上传的文件超过60M,请重新上传");
                return false;
            }
        }



        //表格赋值
        function setPowerInfo(data) {
            dataModel.getDataByTag("table").a("ht.dataSource", data);
        }
        //曲线评价信息
        function setCurveEvaluateInfo(data) {
            if (data != "" && data != undefined) {
                if (data[0].evaluate != undefined && data[0].recommend != undefined) {
                    dataModel.getDataByTag("analysis").a("effect", data[0].evaluate);
                    dataModel.getDataByTag("analysis").a("suggest", data[0].recommend);
                }
            }
        }
        //功率曲线
        function setUnitPowerCurve(data) {
            if (data != "" && data != undefined) {
                if (data.ratePower != undefined && data.revisePower != undefined && data.realPower != undefined && data.speed != undefined) {
                    dataModel.getDataByTag("rate").a("yAxis1", data.ratePower);
                    dataModel.getDataByTag("rate").a("yAxis3", data.revisePower);
                    dataModel.getDataByTag("rate").a("yAxis2", data.realPower);
                    dataModel.getDataByTag("rate").a("xAxis", data.speed);
                }
            }
        }
        function setCalculate(data) {
            if (data != "" && data != undefined) {
                if (data.list != undefined && data.list != '') {
                    dataModel.getDataByTag("table").a("ht.dataSource", data.list);
                }
                if (data.list != undefined && data.list != '') {
                    dataModel.getDataByTag("table").a("ht.dataSource", data.list);
                }
                if (data.realPower != undefined && data.realPower != '') {
                    dataModel.getDataByTag("rate").a("yAxis1", data.ratePower);
                    dataModel.getDataByTag("rate").a("yAxis3", data.revisePower);
                    dataModel.getDataByTag("rate").a("yAxis2", data.realPower);
                    dataModel.getDataByTag("rate").a("xAxis", data.speed);
                }
            }
        }



        /**
         * 监听页面的点击事件 
         * */
        graphView.addInteractorListener(function (e) {
            var data = e.data;
            var kind = e.kind;
            var type = e.type;

            if (kind == 'onClick' && type == "comp") {
                if (data._tag == "introduction") {//点击导入 触发的事件
                    document.getElementById("btn_file").click();

                } if (data._tag == 'calc') {//点击计算
                    calculate("power-curve")//计算接口
                }
                if (data._tag == "search") {//点击搜索 触发的事件
                    var areaData = data._dataModel.getDataByTag("area").a("ht.value");
                    var stationData = data._dataModel.getDataByTag("station").a("ht.value")
                    var setfan = data._dataModel.getDataByTag("fan").a("ht.value")
                    var startTimeData = data._dataModel.getDataByTag("startTime").a("ht.value")
                    var endTimeData = data._dataModel.getDataByTag("endTime").a("ht.value")
                    var start = new Date(startTimeData + " 00:00:00");
                    var end = new Date(endTimeData + " 00:00:00");
                    // console.log(areaData,stationData, setfan, a.valueOf(), endTimeData)
                    unitPowerInfo(setfan, start.valueOf(), end.valueOf())//机组风速功率信息 表格数据
                    curveEvaluateInfo(setfan, start.valueOf(), end.valueOf())//机组功率曲线评价信息
                    unitPowerCurveInfo(setfan, start.valueOf(), end.valueOf())//功率曲线

                }
                if (data._tag == "export") {//点击导出 触发的事件
                    var table = document.getElementById("tableExcel");
                    var column = dataModel.getDataByTag("table").a("ht.columns");
                    var dataSource = dataModel.getDataByTag("table").a("ht.dataSource");

                    var tableHeader = "<tr>";
                    for (var i = 0; i < column.length; i++) {
                        tableHeader += "<td>" + column[i].displayName + "</td>"
                    }

                    tableHeader += '</tr>';

                    var tableBody = "";
                    for (var i = 0; i < dataSource.length; i++) {
                        tableBody += "<tr>"
                        tableBody += "<td>" + dataSource[i].windSpeed + "</td>"
                        tableBody += "<td>" + dataSource[i].ratePower + "</td>"
                        tableBody += "<td>" + dataSource[i].realPower + "</td>"
                        tableBody += "<td>" + dataSource[i].revisePower + "</td>"

                        tableBody += "<td>"
                    }
                    table.innerHTML = tableHeader;
                    table.innerHTML += tableBody;

                    $("#tableExcel").table2excel({
                        name: "sheet1",
                        filename: "机组功率特性曲线-" + new Date().toISOString().replace(/[\-\:\.]/g, "") + ".xls",
                        fileext: ".xls",
                    });
                }
            }

        })

        //监听属性值的变化  主要是用作下拉框联动
        dataModel.addDataPropertyChangeListener(e => {
            var property = e.property;
            var data = e.data;
            var viewUrl;

            if (data._tag == 'area') {//点击区域
                if (property == 'a:valueId') {
                    var id = data.a("valueId")
                    var stationArr = getStation(id);//请求场站接口
                    if (stationArr != "") {
                        //console.log(data)
                        data._dataModel.getDataByTag("station").a("ht.dataSource", stationArr)
                    }
                }
            }
            if (data._tag == 'station') {//点击场站
                if (property == 'a:valueId') {
                    var fanArr = getFan(id);//请求机组接口
                    if (fanArr != "") {

                        data._dataModel.getDataByTag("fan").a("ht.dataSource", fanArr)
                    }
                }
            }
            if (data._tag == 'fan') {//点击机组
                if (property == 'a:valueId') {
                    // console.log("id", data.a("ht.value"))
                    // console.log("label", data.a("ht.label"))
                }
            }

        })




    </script>
</head>

<body onload='init()'>
    <input type="file" name="file1" onchange="showsize(this)" id="btn_file" style="display: none;">
    <table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0" style="display: none;">

    </table>
</body>

</html>
